package com.xidian.zhiancloud.controller.chemicalsManagement.dangerousPart;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.DangerousChemicalInfo;
import com.xidian.zhiancloud.model.DangerousChemicalInfoChange;
import com.xidian.zhiancloud.qo.chemicalsManagement.DangerousChemicalInfoQO;
import com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.dangerousPart.DangerousChemicalInfoService;
import com.xidian.zhiancloud.vo.chemicalsManagement.DangerousChemicalInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 危化品管理 危化品管理
 *
 * @Author: Egoist
 * @Date: 2020/11/10 17:31
 */
@RestController
@RequestMapping("/DangerousChemicalInfo")
public class DangerousChemicalInfoController extends BaseController {

    @Autowired
    private DangerousChemicalInfoService dangerousChemicalInfoService;

    /**
     * 增加危化品信息(危化品名称不能重复)
     *
     * @param dangerousChemicalInfo 危化品信息对象
     * @return 返回时是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(DangerousChemicalInfo dangerousChemicalInfo) {

        dangerousChemicalInfo.setCreateTime(new Date(System.currentTimeMillis()));
        dangerousChemicalInfo.setCompanyId(getSessionUserCompanyId());

        return dangerousChemicalInfoService.saveInfo(dangerousChemicalInfo);
    }

    /**
     * 根据id查找危化品信息
     *
     * @param id 危化品信息对象id
     * @return 危化品信息对象
     */
    @GetMapping("/getById")
    public ResultDTO<DangerousChemicalInfoVO> getById(@RequestParam("id") Long id) {

        return ResultDTO.successOf(dangerousChemicalInfoService.getVOById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id删除危化品信息(废弃）
     * 替换为 hiddenByIds(Long[] ids)
     *
     * @param ids ids数组
     * @return 返回是否删除成功
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {

        return ResultDTO.successOf(dangerousChemicalInfoService.removeByIds(Arrays.asList(ids)), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id将危化品信息记录隐藏
     *
     * @param ids ids数组
     * @return 返回是否隐藏成功
     */
    @PostMapping("/hiddenByIds")
    public ResultDTO<Boolean> hiddenByIds(Long[] ids) {
        return ResultDTO.successOf(dangerousChemicalInfoService.hiddenByIds(Arrays.asList(ids)), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id修改危化品信息(危化品名称不能重复)
     * 只传入 id 与 用户修改过的内容
     * @param dangerousChemicalInfo 危化品信息对象
     * @return 返回是否修改成功
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(DangerousChemicalInfo dangerousChemicalInfo) {
        dangerousChemicalInfo.setCreateTime(null);
        dangerousChemicalInfo.setCompanyId(getSessionUserCompanyId());
        Long userId= getSessionUserid();

        return dangerousChemicalInfoService.updateRecordById(dangerousChemicalInfo, userId);
    }

    /**
     * 根据id查找危化品信息变更记录
     * @param id 危化品信息id
     * @return 危化品信息变更记录列表
     */
    @GetMapping("/getChangeRecordById")
    public ResultDTO<List<DangerousChemicalInfoChange>> getChangeRecordById(Long id) {
        return ResultDTO.successOf(dangerousChemicalInfoService.getChangeRecord(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据品名获取单位
     * @param name 危化品品名
     * @return 单位
     */
    @GetMapping("/getUnitByName")
    public ResultDTO<String> getUnitByName(String name) {
        return ResultDTO.successOf(dangerousChemicalInfoService.getUnitByName(name), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 查找该租户下的所有危化品名称列表
     * @return 危化品id与名称
     */
    @GetMapping("/getChemicalNameList")
    public ResultDTO<Map<Long, String>> getChemicalNameList() {
        Long companyId = getSessionUserCompanyId();
        return ResultDTO.successOf(dangerousChemicalInfoService.getChemicalNameList(companyId), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 列表查询
     *
     * @param dangerousChemicalInfoQO 请求参数
     * @return 返回危化品信息列表
     */
    @GetMapping("getListByQueryObject")
    public ResultDTO<IPage<DangerousChemicalInfoVO>> getListByQueryObject(DangerousChemicalInfoQO dangerousChemicalInfoQO) {
        if (dangerousChemicalInfoQO.getCurrent() == null || dangerousChemicalInfoQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        dangerousChemicalInfoQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(dangerousChemicalInfoService.getListByQueryObject(dangerousChemicalInfoQO), GeneralResponseEnums.SUCCESS);
    }

}
